package com.fuyao.cloud.admin.flow.service.flowable.impl;

import com.alibaba.fastjson.JSONObject;
import com.fuyao.cloud.admin.flow.service.flowable.IDynamicCallbackService;
import com.fuyao.cloud.common.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Service
public class DynamicCallbackServiceImpl implements IDynamicCallbackService {

    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public JSONObject postCallback(String serviceId, String url, Map<String, String> params) {
        return this.callback(serviceId, url, HttpMethod.POST, params);
    }

    @Override
    public JSONObject getCallback(String serviceId, String url, Map<String, String> params) {
        return this.callback(serviceId, url, HttpMethod.GET, params);
    }

    @Override
    public JSONObject callback(String serviceId, String url, HttpMethod method, Map<String, String> params) {
        JSONObject forObject = null;
        ServiceInstance serviceInstance = loadBalancerClient.choose(serviceId);
        if (serviceInstance != null) {
            StringBuilder callbackurl = new StringBuilder();
            if (serviceInstance.isSecure()) {
                callbackurl.append("https://");
            } else {
                callbackurl.append("http://");
            }
            //callbackurl.append(serviceInstance.getHost()).append(":").append(serviceInstance.getPort());
            callbackurl.append(serviceId);
            if (url.charAt(0) == '/') {
                callbackurl.append(url);
            } else {
                callbackurl.append("/").append(url);
            }
            if (HttpMethod.GET.equals(method)) {
                forObject = restTemplate.getForObject(callbackurl.toString(), JSONObject.class, params);
            } else if (HttpMethod.POST.equals(method)) {
                HttpHeaders requestHeaders = getRequestHeaders();
                HttpEntity<Map> requestEntity = new HttpEntity<>(params, requestHeaders);
                forObject = restTemplate.postForObject(callbackurl.toString(), requestEntity, JSONObject.class);
            }
        }
        return forObject;
    }

    private HttpHeaders getRequestHeaders(){
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.APPLICATION_JSON);
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails)SecurityUtils.getAuthentication().getDetails();
        requestHeaders.add("Authorization",details.getTokenType() + " " +details.getTokenValue());
        return requestHeaders;
    }
}

